package com.ruoyi.rycc.service.impl;

import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.rycc.domain.CcChapter;
import com.ruoyi.rycc.mapper.CcChapterMapper;
import com.ruoyi.rycc.service.ICcChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 章节Service业务层处理
 *
 * @author antg
 * @date 2021-09-06
 */
@Service
public class CcChapterServiceImpl implements ICcChapterService {
    @Autowired
    private CcChapterMapper ccChapterMapper;

    /**
     * 查询章节
     *
     * @param id 章节主键
     * @return 章节
     */
    @Override
    public CcChapter selectCcChapterById(Long id, Long courseId) {
        return ccChapterMapper.selectCcChapterById(id, courseId);
    }

    /**
     * 查询章节列表
     *
     * @param ccChapter 章节
     * @return 章节
     */
    @Override
    public List<CcChapter> selectCcChapterList(CcChapter ccChapter) {
        return ccChapterMapper.selectCcChapterList(ccChapter);
    }

    /**
     * 新增章节
     *
     * @param ccChapter 章节
     * @return 结果
     */
    @Override
    public int insertCcChapter(CcChapter ccChapter) {
        ccChapter.setCreateTime(DateUtils.getNowDate());
        return ccChapterMapper.insertCcChapter(ccChapter);
    }

    /**
     * 修改章节
     *
     * @param ccChapter 章节
     * @return 结果
     */
    @Override
    public int updateCcChapter(CcChapter ccChapter) {
        ccChapter.setUpdateTime(DateUtils.getNowDate());
        return ccChapterMapper.updateCcChapter(ccChapter);
    }

    /**
     * 批量删除章节
     *
     * @param ids 需要删除的章节主键
     * @return 结果
     */
    @Override
    public int deleteCcChapterByIds(String ids) {
        return ccChapterMapper.deleteCcChapterByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除章节信息
     *
     * @param id 章节主键
     * @return 结果
     */
    @Override
    public int deleteCcChapterById(Long id) {
        return ccChapterMapper.deleteCcChapterById(id);
    }

    /**
     * 查询章节树列表
     *
     * @return 所有章节信息
     */
    @Override
    public List<Ztree> selectCcChapterTree() {
        List<CcChapter> ccChapterList = ccChapterMapper.selectCcChapterList(new CcChapter());
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (CcChapter ccChapter : ccChapterList) {
            Ztree ztree = new Ztree();
            ztree.setId(ccChapter.getId());
            ztree.setpId(ccChapter.getParentId());
            ztree.setName(ccChapter.getChapterName());
            ztree.setTitle(ccChapter.getChapterName());
            ztrees.add(ztree);
        }
        return ztrees;
    }
}
